package com.py.framework.service.impl;

import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.dao.SysDictDao;
import com.py.framework.entity.DictDetailEntity;
import com.py.framework.entity.DictEntity;
import com.py.framework.service.SysDictService;

/**
 * 字典服务Service实现
 *
 * @author Leegern
 * @date 2018年4月20日
 */
@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictDao sysDictDao;


    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#addDict(com.py.framework.entity.DictEntity)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addDict(DictEntity entity) {
        entity.setDelFlag(Constants.NUMBER_ONE);
        entity.setEnabled(Constants.NUMBER_ONE);
        return sysDictDao.addDict(entity);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#queryDictListForPage(java.lang.String, java.lang.String, com.py.framework.common.Page)
     */
    @Override
    public List<DictEntity> queryDictListForPage(String dictKey, String dictName, Page page) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(dictKey)) {
            params.put("dictKey", dictKey);
        }
        if (StringUtils.isNotBlank(dictName)) {
            params.put("dictName", dictName);
        }
        params.put("page", page);
        return sysDictDao.queryDictListForPage(params);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#fineOneDict(java.lang.String)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public DictEntity fineOneDict(long id) {
        if (id > Constants.NUMBER_ZERO) {
            return sysDictDao.fineOneDict(id);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#batchUpdateDictStatus(java.util.List, int, int)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int batchUpdateDictStatus(List<Long> ids, int enabled, int delFlag) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (!CollectionUtils.isEmpty(ids)) {
            params.put("ids", ids);
        }
        if (enabled > Constants.NUMBER_ZERO) {
            params.put("enabled", enabled);
        }
        if (delFlag > Constants.NUMBER_ZERO) {
            DictEntity detail = null;
            for (Long id : ids) {
                // 判断是否使用中
                detail = this.fineOneDict(id);
                if (null != detail && detail.getEnabled() == Constants.NUMBER_TWO) {
                    throw new ServiceException(detail.getDictName() + "正在启用中，删除失败！");
                }
            }
            params.put("delFlag", delFlag);
        }
        return sysDictDao.batchUpdateDictStatus(params);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#updateDict(com.py.framework.entity.DictEntity)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateDict(DictEntity entity) {
        return sysDictDao.updateDict(entity);
    }


    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#addDictDetail(com.py.framework.entity.DictDetailEntity)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addDictDetail(DictDetailEntity entity) {
        entity.setDelFlag(Constants.NUMBER_ONE);
        entity.setEnabled(Constants.NUMBER_TWO);
        return sysDictDao.addDictDetail(entity);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#queryDictDetailListForPage(java.util.Map)
     */
    @Override
    public List<DictDetailEntity> queryDictDetailListForPage(Map<String, Object> params) {
        if (!CollectionUtils.isEmpty(params)) {
            return sysDictDao.queryDictDetailListForPage(params);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#fineOneDictDetail(java.lang.String)
     */
    @Override
    public DictDetailEntity fineOneDictDetail(long id) {
        if (id > Constants.NUMBER_ZERO) {
            return sysDictDao.fineOneDictDetail(id);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#batchUpdateDictDetailStatus(java.util.List, int, int)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int batchUpdateDictDetailStatus(List<Long> ids, int enabled, int delFlag) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (!CollectionUtils.isEmpty(ids)) {
            params.put("ids", ids);
        }
        if (enabled > Constants.NUMBER_ZERO) {
            params.put("enabled", enabled);
        }
        if (delFlag > Constants.NUMBER_ZERO) {
            DictDetailEntity detail = null;
            for (Long id : ids) {
                // 判断是否使用中
                detail = this.fineOneDictDetail(id);
                if (null != detail && detail.getEnabled() == Constants.NUMBER_TWO) {
                    throw new ServiceException(detail.getDtlKey() + "正在启用中，删除失败！");
                }
            }
            params.put("delFlag", delFlag);
        }
        return sysDictDao.batchUpdateDictDetailStatus(params);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#updateDictDetail(com.py.framework.entity.DictDetailEntity)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateDictDetail(DictDetailEntity entity) {
        return sysDictDao.updateDictDetail(entity);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#getDictDetailByKey(long, long, java.lang.String)
     */
    @Override
    public List<DictDetailEntity> getDictDetailByKey(long dictId, long dictDetailId, String dtlKey) {
        Map<String, Object> params = new HashMap<>();
        params.put("dictId", dictId);
        params.put("dictDetailId", dictDetailId);
        params.put("dtlKey", dtlKey);
        return sysDictDao.getDictDetailByKey(params);
    }

    /*
     * (non-Javadoc)
     * @see com.py.framework.service.SysDictService#getDictDetails(java.lang.String, java.lang.String)
     */
    @Override
    public List<DictDetailEntity> getDictDetails(String dictKey, String dtlKey) {
        if (StringUtils.isNotBlank(dictKey)) {
            List<DictDetailEntity> list = new ArrayList<>();
            try {
                list = sysDictDao.getDictDetails(dictKey, dtlKey);
            } catch (Exception e) {
                System.out.printf(e.toString());
                return null;
            }
            return list;
        }
        return null;
    }

    @Override
    public List<LinkedHashMap> seletctDictInfo() {
        return sysDictDao.seletctDictInfo();
    }

	@Override
	public List<DictEntity> fineSameDict(String dictKey) {
		return sysDictDao.fineSameDict(dictKey);
	}
}